# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
#################################################
# 梯度计算封装为函数
def cv_show(img,name,writename):

    #计算左右边界
    sobelx1 = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)  # 写成能表示负数的形式，且dx=1，dy=0表示计算的是水平方向
    sobelx2 = cv2.convertScaleAbs(sobelx1)  # 取绝对值
    #计算上下边界
    sobely1 = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)  #
    sobely2 = cv2.convertScaleAbs(sobely1)
    #边界相加
    sobelxy = cv2.addWeighted(sobelx2, 0.5, sobely2, 0.5, 0)  # 相加的系数根据项目修改
    res = np.hstack((img, sobelxy))
    cv2.imshow(name,res)
    cv2.imwrite(writename,res)
    cv2.waitKey()
    cv2.destroyAllWindows()



img = cv2.imread('picture.png',0)
cv_show(img,'sdff','test.png')